﻿using System.Runtime.CompilerServices;

namespace LxBasic
{
    /// <summary>
    /// 累加型数字，空数字可加减，不可乘除
    /// </summary>
    public struct AccumulationDouble
    {
        public AccumulationDouble()
        {
            isEmpty = true;
            value = 0;
        }
        public AccumulationDouble(double iniValue)
        {
            isEmpty = false;
            value = iniValue;
        }
        internal bool isEmpty;
        internal double value;
        public static AccumulationDouble Empty => new AccumulationDouble();
        public bool IsEmpty => isEmpty;
        public double Value => value;


        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static AccumulationDouble operator +(AccumulationDouble left, AccumulationDouble right)
        {
            return left.isEmpty && right.isEmpty ? Empty : new AccumulationDouble(left.value + right.value);
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static AccumulationDouble operator +(AccumulationDouble left, double right)
        {
            return new AccumulationDouble(left.Value + right);
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static AccumulationDouble operator +(double left, AccumulationDouble right)
        {
            return right + left;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static AccumulationDouble operator -(AccumulationDouble d)
        {
            return d.isEmpty ? Empty : new AccumulationDouble(d.Value);
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static AccumulationDouble operator -(AccumulationDouble left, AccumulationDouble right)
        {
            return left.isEmpty && right.isEmpty ? Empty : new AccumulationDouble(left.value - right.value);
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static AccumulationDouble operator *(AccumulationDouble left, AccumulationDouble right)
        {
            return left.isEmpty || right.isEmpty ? Empty : new AccumulationDouble(left.value * right.value);
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static AccumulationDouble operator *(AccumulationDouble left, double right)
        {
            return left.isEmpty ? Empty : new AccumulationDouble(left.value * right);
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static AccumulationDouble operator *(double left, AccumulationDouble right)
        {
            return right * left;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static AccumulationDouble operator /(AccumulationDouble left, AccumulationDouble right)
        {
            return left.isEmpty || right.isEmpty ? Empty : new AccumulationDouble(left.value / right.value);
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static AccumulationDouble operator /(AccumulationDouble left, double right)
        {
            return left.isEmpty ? Empty : new AccumulationDouble(left.value / right);
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static AccumulationDouble operator /(double left, AccumulationDouble right)
        {
            return right.isEmpty ? Empty : new AccumulationDouble(left / right.Value);
        }
        public static implicit operator AccumulationDouble(double value)
        {
            return new AccumulationDouble(value);
        }
        public override string ToString()
        {
            return isEmpty ? "无数据" : value.ToString();
        }
    }
}
